package com.litong.litongjiaoyu.controller;

import com.alibaba.fastjson.JSONObject;
import com.litong.litongjiaoyu.annotation.IgnoreAuth;
import com.litong.litongjiaoyu.entity.SysOrder;
import com.litong.litongjiaoyu.service.OrderService;
import com.litong.litongjiaoyu.utils.JSONResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName OrderController
 * @Description 订单系统控制层
 * @Author jingz
 * @Date 2019/12/29 1:32
 * @Version 1.0
 **/
@RestController
@RequestMapping("/order")
public class OrderController {
    /**
     * 门面模式引入log
     */
    private Logger logger = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private OrderService orderService;


    /**
     * 根据校区获取订单信息
     * @param campus
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @PostMapping("")
    public JSONResult query(String campus){
        logger.info("开始根据校区获取订单信息");

        try {
            List<SysOrder> orders = orderService.query(campus);
            logger.info(String.format("获取成功，数据总长度为：%d", orders.size()));
            if (orders.size() != 0) {
                return JSONResult.ok(orders);
            } else {
                return JSONResult.ok(true);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return JSONResult.errorException(e.getMessage());
        }
    }
    
    /**
     * 获取购买历史记录
     * @param jsonObject 手机号
     * @return 历史记录json
     */
    @ResponseBody
    @PostMapping("/gethistoryrecord")
    public JSONResult getHistoryRecord(@RequestBody JSONObject jsonObject) {
        List<SysOrder> orders = orderService.getPurchaseHistory(jsonObject.getString("phone"));
    
        if (orders.size() > 0) {
            return JSONResult.ok(orders);
        } else {
            return JSONResult.ok(null);
        }
    }

    /**
     * 获取当前订单信息
     * @param Id
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @PostMapping("/getById")
    public JSONResult getOrderById(String Id){
        logger.info(String.format("开始获取订单编号为：%s的订单详细信息", Id.toString()));

        try {
            SysOrder sysOrder = orderService.getOrderById(Id);
            logger.info("订单信息如下：" + sysOrder.toString());


            if (sysOrder != null) {
                return JSONResult.ok(sysOrder);

            } else {
                return JSONResult.errorMsg("没有改订单编号内容，请检查是否有误！");
            }
        } catch (Exception e) {

            logger.error("getOrderById方法出现错误：" + e.getMessage());

            return JSONResult.ok(e.getMessage());
        }

    }

    /**
     * 生成订单
     * @param jsonObject sysOrder
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @PostMapping("/addOrder")
    public JSONResult addOrder(@RequestBody JSONObject jsonObject) {
        logger.info("开始添加订单内容！");

        SysOrder sysOrder = jsonObject.getObject("order", SysOrder.class);
        try {
            if (sysOrder != null) {
                logger.info("\t > 数据不为空!");
                if(orderService.addOrder(sysOrder) > 0) {
                    logger.info("\t\t > >数据添加成功");
                    return JSONResult.ok();
                } else {
                    logger.warn("\t\t > >数据添加失败！检查是否有错误信息");
                    return JSONResult.errorMsg("添加失败！");
                }
            } else {
                logger.error("\t > 数据为空，无法添加至数据库！");
                return JSONResult.errorException("数据为空，无法添加至数据库");
            }
        } catch (Exception e) {

            logger.error("\t > addOrder方法出现错误：" + e.getMessage());

            return JSONResult.ok(e.getMessage());
        }
    }

    @ResponseBody
    @PostMapping("/updateOrder")
    public JSONResult updateOrder(@RequestBody SysOrder sysOrder){
        System.out.println(sysOrder);
        logger.info("开始修改订单内容！");

        try {

            if (sysOrder != null) {
                logger.info("\t > 数据不为空!");
                if(orderService.updateOrder(sysOrder) > 0) {
                    logger.info("\t\t > > 数据修改成功");
                    return JSONResult.ok();
                } else {
                    logger.warn("\t\t > >数据修改失败！检查是否有错误信息");
                    return JSONResult.errorMsg("修改失败！");
                }
            } else {
                logger.error("\t > 数据为空，无法修改！");
                return JSONResult.errorException("数据为空，无法添加修改");
            }
        } catch (Exception e) {

            logger.error("\t > updateOrder方法出现错误：" + e.getMessage());

            return JSONResult.ok(e.getMessage());
        }
    }

    /**
     * 删除单个订单
     * @param Id
     * @return
     */
    public JSONResult deleteOne(String Id) {
        logger.info(String.format("开始删除订单:%s！", Id).toString());

        try {

            if (Id != null || Id != "") {
                logger.info("\t > 数据不为空!");
                if(orderService.deleteOne(Id) > 0) {
                    logger.info("\t\t > > 订单删除成功");
                    return JSONResult.ok();
                } else {
                    logger.warn("\t\t > >订单删除失败！检查是否有错误信息");
                    return JSONResult.errorMsg("订单删除");
                }
            } else {
                logger.error("\t > 数据为空，无法删除！");
                return JSONResult.errorException("数据为空，无法删除");
            }
        } catch (Exception e) {

            logger.error("\t > deleteOne方法出现错误：" + e.getMessage());

            return JSONResult.ok(e.getMessage());
        }
    }


    /**
     * 删除所有订单
     * @return
     */
    public JSONResult deleteAll(){
        logger.info("开始删除所有订单信息");

        try {
            if(orderService.deleteAll() > 0) {
                logger.info("\t > 删除所有订单信息成功");
                return JSONResult.ok();
            } else {
                logger.warn("\t > 删除所有订单信息失败");
                return JSONResult.errorMsg("删除所有订单信息失败");
            }

        } catch (Exception e) {

            logger.error("\t > deleteAll方法出现错误：" + e.getMessage());

            return JSONResult.ok(e.getMessage());
        }
    }

}
